package cn.lopr.im.android.utils;

import android.text.format.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
import java.util.TimeZone;

/**
 * @author shuangyang7
 * @date 2021/1/3 17:01
 */
public class DateUtil {

    public static SimpleDateFormat hmsFormat = new SimpleDateFormat("HH:mm:ss");
    public static SimpleDateFormat dmyFormat = new SimpleDateFormat("dd/MM/yy");
    public static SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy/MM/dd");
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getTime() {
        //设置时区，如：+8:00 或者取时区id(GMT)
        TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(time);

        Calendar calendar = Calendar.getInstance();
        //获取Date对象
        Date date = calendar.getTime();
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return format.format(date);
    }

    /**
     * @return String res
     */
    public static String getTimeDesc(Date date) {
        //设置时区，如：+8:00 或者取时区id(GMT)
        TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(time);

        Calendar calendar = Calendar.getInstance();
        //获取Date对象
        calendar.setTime(date);
        date = calendar.getTime();
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));


        return format.format(date);
    }

    public static String formatYMD(Date date) {
        return ymdFormat.format(date);
    }

    public static String formatYMD(String date) {
        try {
            return formatYMD(format.parse(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formatDMY(Date date) {
        return dmyFormat.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String formatDMY(String date) {
        try {
            return formatDMY(format.parse(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String formatHMS(Date date) {
        return hmsFormat.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String formatHMS(String date) {
        try {
            return formatHMS(format.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date formatYMDHMS(String date) {
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formatYMDHMS(Date date) {
        return format.format(date);
    }

    public static boolean isCurrentDay(String date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            return DateUtils.isToday(Objects.requireNonNull(sdf.parse(date)).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
